package com.emotion.emotional_damage.service.impl;

import com.emotion.emotional_damage.mapper.AnalysisMapper;
import com.emotion.emotional_damage.service.AnalysisService;
import com.emotion.emotional_damage.tool.PinYinUtil;
import org.ansj.splitWord.analysis.NlpAnalysis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: HAPPY
 * @Project_name: emotional_damage
 * @Package_name: com.emotion.emotional_damage.service.impl
 * @Date: 2022/3/13 9:13
 * @Description:
 */
@Service
public class AnalysisServiceImpl implements AnalysisService {
    @Autowired
    AnalysisMapper analysisMapper;

    @Override
    public Map<String, List<String>> analysis(String text) {
        // 将字符串进行分词，对每个关键词进行查询
        String[] res = NlpAnalysis.parse(text).toString().split(",");
        Map<String, List<String>> resList = new HashMap<>();

        for (int i = 0; i < res.length; i++) {
            System.out.println(res[i]);
            // 将关键词转为拼音查询
            String analyse = PinYinUtil.getPinyin(res[i], PinYinUtil.LOWERCASE);
            // 去除标签
            String keywords = analyse.substring(0, analyse.indexOf("/"));
            System.out.println(keywords);
            resList.put(keywords, analysisMapper.matchingKey(keywords));
        }
        return resList;
    }
}
